package com.atguigu.eduService.service.impl;

import com.atguigu.eduService.entity.EduCourse;
import com.atguigu.eduService.entity.EduCourseDescription;
import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.entity.frontvo.CourseFrontVo;
import com.atguigu.eduService.entity.frontvo.CourseWebVo;
import com.atguigu.eduService.entity.vo.CourseInfoVo;
import com.atguigu.eduService.entity.vo.CoursePublishVo;
import com.atguigu.eduService.entity.vo.CourseQuery;
import com.atguigu.eduService.mapper.EduChapterMapper;
import com.atguigu.eduService.mapper.EduCourseDescriptionMapper;
import com.atguigu.eduService.mapper.EduCourseMapper;
import com.atguigu.eduService.mapper.EduVideoMapper;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduCourseDescriptionService;
import com.atguigu.eduService.service.EduCourseService;
import com.atguigu.eduService.service.EduVideoService;
import com.atguigu.eduService.utils.PageUtils;
import com.atguigu.serviceBase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author smday
 * @since 2020-07-27
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionMapper courseDescriptionMapper;

    @Resource
    private EduCourseMapper courseMapper;

    @Resource
    private EduVideoService videoService;

    @Resource
    private EduChapterService chapterService;

    @Override
    public String addCourse(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        //向课程表中添加课程基本信息
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException("添加课程信息失败");
        }
        String courseId = eduCourse.getId();
        //向课程简介表中添加课程间界
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        //设置描述id为课程id
        description.setId(courseId);
        courseDescriptionMapper.insert(description);
        return courseId;
    }

    @Override
    public CourseInfoVo getInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //查询描述表
        EduCourseDescription description = courseDescriptionMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new GuliException("修改课程信息失败");
        }
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionMapper.updateById(description);
    }

    @Override
    public CoursePublishVo getPublishCourse(String courseId) {
        return courseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public IPage<EduCourse> pageQuery(Integer current, Integer size, CourseQuery courseQuery) {

        Page<EduCourse> page = new Page<>(current,size);

        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(courseQuery.getTitle())){
            wrapper.like("title",courseQuery.getTitle());
        }
        if(!StringUtils.isEmpty(courseQuery.getStatus())){
            wrapper.eq("status",courseQuery.getStatus());
        }
        if(!StringUtils.isEmpty(courseQuery.getBegin())){
            wrapper.ge("gmt_create",courseQuery.getBegin());
        }
        if(!StringUtils.isEmpty(courseQuery.getEnd())){
            wrapper.le("gmt_create",courseQuery.getEnd());
        }
        wrapper.orderByDesc("gmt_create");
        return baseMapper.selectPage(page, wrapper);


    }

    @Override
    public void deleteCourse(String courseId) {

        //根据课程id删除小节
        videoService.deleteVideoByCourseId(courseId);

        //根据课程id删除章节
        chapterService.deleteChapterByCourseId(courseId);

        //根据课程id删除描述
        courseDescriptionMapper.deleteById(courseId);

        //根据课程id删除本身
        int delete = baseMapper.deleteById(courseId);
        if(delete == 0){
            throw new GuliException("删除失败");
        }

    }

    @Cacheable(value = "guli:cms:course",key = "'course'")
    @Override
    public List<EduCourse> listCourse() {
        QueryWrapper<EduCourse> wrapperCourse = new QueryWrapper<>();
        wrapperCourse.orderByDesc("id");
        wrapperCourse.last("limit 8");
        return baseMapper.selectList(wrapperCourse);
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }

        baseMapper.selectPage(pageCourse, wrapper);

        Map<String, Object> map = PageUtils.convertPageToMap(pageCourse);
        return map;


    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
